package com.viettel.rmsmobile;

import java.util.List;
import java.util.Locale;

import com.viettel.rmsmobile.R;
import com.viettel.synctask.LoginSyncTask;
import com.viettel.BO.PortalUserBean;
import com.viettel.menu.MenuAdapter;
import com.viettel.menu.MenuDTO;
import com.viettel.menu.PlanetFragment;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements
		SearchView.OnQueryTextListener {

	private DrawerLayout mainLayout;
	private ListView listViewMenu;
	private ActionBarDrawerToggle mDrawerToggle;

	private String title;
	public static MenuDTO menu_data[];

	private SearchView mSearchView;
	private TextView mStatusView;

	public static Long portalUserId = null;
	public static PortalUserBean portalUser = new PortalUserBean();

	EditText userName;
	EditText password;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mStatusView = (TextView) findViewById(R.id.status_text);
		menu_data = new MenuDTO[] {
				new MenuDTO(R.drawable.recruitment, getResources().getString(
						R.string.findJob)),
				new MenuDTO(R.drawable.save_job_icon, getResources().getString(
						R.string.saveJob)),
				new MenuDTO(R.drawable.apply_job, getResources().getString(
						R.string.applyJob)),
				new MenuDTO(R.drawable.upload_profile, getResources()
						.getString(R.string.uploadImageProfile)),
				new MenuDTO(R.drawable.account, getResources().getString(
						R.string.accountInfo)) };

		MenuAdapter adapter = new MenuAdapter(this,
				R.layout.list_item_left_menu, menu_data);

		title = (String) getTitle();// tieu app
		mainLayout = (DrawerLayout) findViewById(R.id.main_layout);
		listViewMenu = (ListView) findViewById(R.id.lefMenu);

		View header = (View) getLayoutInflater().inflate(
				R.layout.list_menu_header, null);
		listViewMenu.addHeaderView(header);
		// set a custom shadow that overlays the main content when the drawer
		// opens
		mainLayout.setDrawerShadow(R.drawable.drawer_shadow,
				GravityCompat.START);
		// set up the drawer's list view with items and click listener
		listViewMenu.setAdapter(adapter);
		listViewMenu.setOnItemClickListener(new DrawerItemClickListener());

		// enable ActionBar app icon to behave as action to toggle nav drawer
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

		// ActionBarDrawerToggle ties together the the proper interactions
		// between the sliding drawer and the action bar app icon
		mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
		mainLayout, /* DrawerLayout object */
		R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
		R.string.drawer_open, /* "open drawer" description for accessibility */
		R.string.drawer_close /* "close drawer" description for accessibility */
		) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(title);
				invalidateOptionsMenu(); // creates call to
											// onPrepareOptionsMenu()
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(title);
				invalidateOptionsMenu(); // creates call to
											// onPrepareOptionsMenu()
			}
		};
		// mainLayout.setDrawerListener(mDrawerToggle);

		if (savedInstanceState == null) {
			selectItem(1);
		}
		// getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
	}

	private void setupSearchView(MenuItem searchItem) {

		if (isAlwaysExpanded()) {
			mSearchView.setIconifiedByDefault(false);
		} else {
			searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
					| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
		}

		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		if (searchManager != null) {
			List<SearchableInfo> searchables = searchManager
					.getSearchablesInGlobalSearch();

			SearchableInfo info = searchManager
					.getSearchableInfo(getComponentName());
			for (SearchableInfo inf : searchables) {
				if (inf.getSuggestAuthority() != null
						&& inf.getSuggestAuthority().startsWith("applications")) {
					info = inf;
				}
			}
			mSearchView.setSearchableInfo(info);
		}

		mSearchView.setOnQueryTextListener(this);
	}

	protected boolean isAlwaysExpanded() {
		return false;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// If the nav drawer is open, hide action items related to the content
		// view
		boolean drawerOpen = mainLayout.isDrawerOpen(listViewMenu);
		menu.findItem(R.id.action_search).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.searchview_in_menu, menu);
		MenuItem searchItem = menu.findItem(R.id.action_search);
		mSearchView = (SearchView) searchItem.getActionView();
		setupSearchView(searchItem);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// The action bar home/up action should open or close the drawer.
		// ActionBarDrawerToggle will take care of this.
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		// Handle action buttons
		switch (item.getItemId()) {
		case android.R.id.home:
			// Toast.makeText(this, "Back !", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.action_search:
			// create intent to perform web search for this planet
			Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG)
					.show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/* The click listner for ListView in the navigation drawer */
	private class DrawerItemClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			selectItem(position);
		}
	}

	/**
	 * Select Item menu
	 * 
	 * @param position
	 */
	public void selectItem(int position) {
		// update the main content by replacing fragments
		Fragment fragment;
		if (position == 1) {
			fragment = new FindJob();
		} else if (position == 3) {
			fragment = new CandidateCareerFragment();
		} else if (position == 5) {
			fragment = new UserLoginActivity();
		} else {
			fragment = new PlanetFragment();
		}
		Bundle args = new Bundle();
		args.putInt(PlanetFragment.ARG_POSITION, position);
		fragment.setArguments(args);

		if (position > 0) {
			MenuDTO menuCurrent = menu_data[position - 1];
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame, fragment).commit();
			title = menuCurrent.getMenuName();
			listViewMenu.setItemChecked(position, true);
			setTitle(title);
			getActionBar().setTitle(title);
		}
		mainLayout.closeDrawer(listViewMenu);
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		// TODO Auto-generated method stub
		// mStatusView.setText("Query = " + newText);
		return false;
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		mStatusView.setText("Query = " + query + " : submitted");
		return false;
	}

	public boolean onClose() {
		mStatusView.setText("Closed!");
		return false;
	}

}
